<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/20
 * Time: 下午5:08
 */

namespace app\admin\controller;


use app\src\account\enmu\AccountEnmu;
use app\src\accountScore\logic\AccountScoreLogic;
use app\src\filiation\action\FiliationAction;
use app\src\platform\logic\PlatformLogic;
use app\src\powersystem\enum\AdminRoleEnum;
use app\src\powersystem\logic\AuthGroupAccessLogic;
use app\src\profit\logic\ProfitLogic;
use app\src\profit\model\Profit;
use app\src\seller\emnu\SellerOrderStatusEmnu;
use app\src\seller\logic\SellerLogic;
use app\src\sellerApply\logic\SellerApplyLogic;
use app\src\sellerCategory\logic\SellerCategoryLogic;
use app\src\user\logic\VUserInfoLogic;

class Seller extends Admin
{
    /**
     * 获取个人创业平台下所有商家
     */
    public function querySeller()
    {
        $platform_id = $this->_param('platform_id');
        if(empty($platform_id)) $this->error('平台id缺失');
        //查询平台名称
        $result = (new PlatformLogic)->getPlatformInfo($platform_id);
        if(!$result['status']) $this->error('获取商家失败', null, $result['info']);
        $platform_name = $result['info']['name'];
        $result = (new SellerLogic)->childAll($platform_id);
        if($result['status']){
            $list = [];
            foreach ($result['info'] as $seller){
                $result = (new FiliationAction())->memberAndMoney($seller['id'],2);

                     $seller['score'] = $result['info']['account_sum'];
                     $seller['member_count'] = $result['info']['account_sum'];
                     $list[] = $seller;
            }
            $this->success('获取商家成功', null, [
                'parent_name' => $platform_name,
                'list' => $list
            ]);
        }else{
            $this->error('获取商家失败,', null, $result['info']);
        }
    }

    /**
     * 获取商家信息
     */
    public function getInfo()
    {
        $seller_id = $this->_param('seller_id');
        if(empty($seller_id)) $this->error('商家id缺失');
        $result = (new SellerLogic)->getSellerInfo($seller_id);
        if($result['status']){
            $seller_Info = $result['info'];

            //店小二信息
            $result = (new VUserInfoLogic)->getInfo(['id' => $seller_Info['waiter_id']]);
            if(!$result['status']) $this->error('获取商家信息失败', null, $result['info']);
            $waiter_info = [];
            if(!empty($result['info'])){
                $info = $result['info'];
                $waiter_info[] = [
                    'uid' => $info['id'],
                    'mobile' => $info['mobile'],
                    'nickname' => $info['nickname']
                ];
            }
            $seller_Info['waiter_info'] = $waiter_info;

            //商家管理员信息
            $result = (new VUserInfoLogic)->getInfo(['id' => $seller_Info['uid']]);
            if(!$result['status']) $this->error('获取商家信息失败', null, $result['info']);
            $admin_info = [];
            if(!empty($result['info'])){
                $info = $result['info'];
                $admin_info[] = [
                    'uid' => $info['id'],
                    'mobile' => $info['mobile'],
                    'nickname' => $info['nickname']
                ];
            }
            //商家分类信息
            $cate_name = '无';
            $category_id = $seller_Info['category_id'];
            if($category_id > 0) {
                $result = (new SellerCategoryLogic)->getFullCategoryName($category_id);
                if($result['status']) $cate_name = $result['info'];
            }
            $seller_Info['cate_name'] = $cate_name;

            $seller_Info['admin_info'] = $admin_info;

            $this->success('获取商家信息成功', null, $seller_Info);
        }else{
            $this->error('获取商家信息失败', null, $result['info']);
        }
    }

    /**
     * 设置店小二
     */
    public function setWaiter()
    {
        $seller_id = $this->_param('seller_id');
        $mobile = $this->_param('mobile');
        if(empty($seller_id)) $this->error('商家id缺失');
        if(empty($mobile)) $this->error('店小二手机号缺失');

        //查询该手机用户id
        $result = (new VUserInfoLogic)->getInfo(['mobile' => $mobile]);
        if(!$result['status'])  $this->error('设置店小二失败', null, $result['info']);
        if(empty($result['info'])) $this->error('不存在的用户', null);
        $uid = $result['info']['id'];

        $result = (new SellerLogic)->setWaiter($seller_id, $uid);
        if($result['status']){
            $this->success('设置店小二成功', null, $result['info']);
        }else{
            $this->error($result['info'], null, $result['info']);
        }
    }

    /**
     * 移除店小二
     */
    public function removeWaiter()
    {
        $seller_id = $this->_param('seller_id');
        $uid = $this->_param('uid');
        if(empty($seller_id)) $this->error('商家id缺失');
        if(empty($uid)) $this->error('店小二id缺失');

        $result = (new SellerLogic)->removeWaiter($seller_id, $uid);
        if($result['status']){
            $this->success('移除店小二成功', null, $result['info']);
        }else{
            $this->error('移除店小二失败', null, $result['info']);
        }
    }

    /**
     * 删除商家
     */
    public function deleteSeller()
    {
        $seller_id = $this->_param('seller_id');
        if(empty($seller_id)) $this->error('商家id缺失');

        $result = (new SellerLogic)->deleteSeller($seller_id);

        if($result['status']){
            $this->success('删除商家成功', null, $result['info']);
        }else{
            $this->error('删除商家失败', null, $result['info']);
        }
    }

    public function lockFirstOffer()
    {
        $seller_id = $this->_param('seller_id');
        if(empty($seller_id)) $this->error('商家id缺失');
        $lock = $this->_param('lock', '');
        if($lock == '') $this->error('lock参数缺失');

        $entity = [
            'lock_first_offer' => $lock ? 1 : 0
        ];
        $result = (new SellerLogic)->updateSeller($seller_id, $entity);

        if($result['status']){
            $this->success('操作成功', null, $result['info']);
        }else{
            $this->error('操作失败', null, $result['info']);
        }
    }

    /**
     * 商家申请管理
     */
    public function queryApply()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            //查询未处理申请
            $p = $this->_param('p', 1);
            $size = $this->_param('size', 15);
            $page = ['curpage' => $p, 'size' => $size];
            $result = (new SellerApplyLogic)->applyList($page);
            if($result['status']){
                //上级平台，所属省份，所属城市
                $apply = $result['info'];
                $list = [];
                $PlatformLogic = (new PlatformLogic);
                foreach ($apply['list'] as $val){
                    //父级名称
                    $result = $PlatformLogic->getPlatformInfo($val['parent_id']);
                    if(!$result['status']) $this->error('获取商家申请列表失败', null, $result['info']);
                    $val['parent_name'] = $result['info']['name'];
                    $list[] = $val;

                }
                $apply['list'] = $list;
                $this->success('获取商家申请列表成功', null, $apply);
            }else{
                $this->error('获取商家申请列表失败', null, $result['info']);
            }
        }
    }

    /**
     * 获取商家申请信息
     */
    public function getApplyInfo()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('商家申请id缺失');
            $result = (new SellerApplyLogic)->applyDetail($apply_id);
            if($result['status']){
                $apply_info = $result['info'];
                //商家分类信息
                $cate_name = '无';
                $category_id = $apply_info['category_id'];
                if($category_id > 0) {
                    $result = (new SellerCategoryLogic)->getFullCategoryName($category_id);
                    if($result['status']) $cate_name = $result['info'];
                }
                $apply_info['cate_name'] = $cate_name;
                $this->success('获取商家申请信息成功', null, $apply_info);
            }else{
                $this->error('获取商家申请信息失败', null, $result['info']);
            }

        }
    }

    /**
     * 商家申请通过/否决
     */
    public function passApply()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }
            $pass = $this->_param('pass');
            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('商家申请id缺失');
            //1通过 2否决
            if(!in_array($pass,[1,2])) $this->error('pass error');
            $pass = $pass == 1;

            $result = (new SellerApplyLogic)->passApply($apply_id, $pass);
            if($result['status']){
                $this->success('操作成功', null, $result['info']);
            }else{
                $this->error($result['info'], null, $result['info']);
            }
        }
    }

    /**
     * 商家订单管理
     */
    public function querySellerSum()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $p = $this->_param('p', 1);
            $size = $this->_param('size', 15);
            $page = ['curpage' => $p, 'size' => $size];

            $date = $this->_param('date');

            $dateType = $this->_param('dateType');
            if(empty($date)) $this->error('date 缺失');
            if(empty($dateType)) $this->error('dateType 缺失');



            switch ($dateType) {
                case 'day':
                    $start = strtotime(date('Y-m-d', $date));
                    $end = strtotime('next day', $start);
                    break;
                case 'week':
                    $start = strtotime(date('Y-m-d', $date));
                    $end = strtotime('next week', $start);
                    break;
                case 'month':
                    $start = strtotime(date('Y-m-d', $date));
                    $end = strtotime('next month', $start);
                    break;
                case 'daterange':
                    $date = explode(",", $date);
                    $start = $date[0];
                    $end = $date[1];
                    break;
                default:
                    $this->error('dateType 错误');
                    return;
            }

            $map = [
                'so.create_time' => [['>=', $start], ['<', $end]],
                'so.order_status' => ['=', SellerOrderStatusEmnu::PAY]
            ];
            $order = [
                'order_count' => 'desc'
            ];
            $result = (new SellerLogic)->querySellerOrderSum($map, $page, $order);

            if($result['status']){
                //获取商家开户行 银行卡号
                $info = $result['info'];
                $SellerLogic = new SellerLogic;
                foreach ($info['list'] as &$val){
                    $result = $SellerLogic->getSellerInfo($val['seller_id']);
                    if($result['status']){
                        $val['bank_card_name'] = $result['info']['bank_card_name'];
                        $val['bank_card_no'] = $result['info']['bank_card_no'];
                        $val['bank_card_no'] = $result['info']['bank_card_no'];
                    }


                    $map_order = [
                        'so.seller_id' => ['=', $val['seller_id']],
                        'so.create_time' => [['>=', $start], ['<', $end]],
                        'so.order_status' => ['=', SellerOrderStatusEmnu::PAY]
                    ];
                    $result2 = $SellerLogic->querySellerOrder($map_order, ['curpage' => 1, 'size' => 999999]);
                    $orders = [];
                    foreach ($result2['info']['list'] as $item){
                        $orders[] = $item['id'];
                    }
                    $s0 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s0');
                    $s1 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s1');
                    $s1_5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s1_5');
                    $s2 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s2');
                    $s3 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s3');
                    $s4 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s4');
                    $s5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s5');
                    $s6 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s6');
                    $s7 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s7');
                    $surplus = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'surplus');

                    $val['s0'] = $s0['info'];
                    $val['s1'] = $s1['info'];
                    $val['s1_5'] = $s1_5['info'];
                    $val['s2'] = $s2['info'];
                    $val['s3'] = $s3['info'];
                    $val['s4'] = $s4['info'];
                    $val['s5'] = $s5['info'];
                    $val['s6'] = $s6['info'];
                    $val['s7'] = $s7['info']+$surplus['info'];


                }
                $this->success('获取订单统计数据成功', null, $info);
            }else{
                $this->error('获取订单统计数据失败', null, $result['info']);
            }
        }
    }

    public function querySellerOrder()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $p = $this->_param('p', 1);
            $size = $this->_param('size', 15);
            $page = ['curpage' => $p, 'size' => $size];
            $seller_id = $this->_param('seller_id');
            $date = $this->_param('date');
            $dateType = $this->_param('dateType');
            if(empty($seller_id)) $this->error('seller_id 缺失');
            if(empty($date)) $this->error('date 缺失');
            if(empty($dateType)) $this->error('dateType 缺失');

            switch ($dateType) {
                case 'day':
                    $start = strtotime(date('Y-m-d', $date));
                    $end = strtotime('next day', $start);
                    break;
                case 'week':
                    $start = strtotime(date('Y-m-d', $date));
                    $end = strtotime('next week', $start);
                    break;
                case 'month':
                    $start = strtotime(date('Y-m-d', $date));
                    $end = strtotime('next month', $start);
                    break;
                case 'daterange':
                    $date = explode(",", $date);
                    $start = $date[0];
                    $end = $date[1];
                    break;
                default:
                    $this->error('dateType 错误');
                    return;
            }
            $map = [
                'so.seller_id' => ['=', $seller_id],
                'so.create_time' => [['>=', $start], ['<', $end]],
                'so.order_status' => ['=', SellerOrderStatusEmnu::PAY]
            ];
            $order = [
                'so.create_time' => 'desc'
            ];
            $result = (new SellerLogic)->querySellerOrder($map, $page, $order);
            $result2 = (new SellerLogic)->querySellerOrder($map, ['curpage' => 1, 'size' => 999999], $order);
            if($result['status']){
                $info = $result['info'];
                //商家名称
                $result = (new SellerLogic)->getSellerInfo($seller_id);
                $orders = [];

                foreach ($result2['info']['list'] as $item){
                    $orders[] = $item['id'];
                }
                foreach ($info['list'] as &$item){
                    $s0 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s0');
                    $s1 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s1');
                    $s1_5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s1_5');
                    $s2 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s2');
                    $s3 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s3');
                    $s4 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s4');
                    $s5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s5');
                    $s6 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s6');
                    $s7 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s7');
                    $surplus = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'surplus');
                    $item['s0'] = $s0['info'];
                    $item['s1'] = $s1['info'];
                    $item['s1_5'] = $s1_5['info'];
                    $item['s2'] = $s2['info'];
                    $item['s3'] = $s3['info'];
                    $item['s4'] = $s4['info'];
                    $item['s5'] = $s5['info'];
                    $item['s6'] = $s6['info'];
                    $item['s7'] = $s7['info']+$surplus['info'];
                    $item['seller_pr'] = '用户获利:'.($s0['info']/100).'元,用户上级获利:'.($s1['info']/100).'元,用户上上级获利:'.($s1_5['info']/100).'元,商家获利:'.($s2['info']/100).'元,店小二获利:'.($s3['info']/100).'元,个人创业获利:'.($s4['info']/100).'元,城市服务中心获利:'.($s5['info']/100).'元,省服务中心获利:'.($s6['info']/100).'元,有车有保获利:'.(($s7['info']+$surplus['info'])/100).'元';
                }

                $s0 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s0');
                $s1 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s1');
                $s1_5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s1_5');
                $s2 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s2');
                $s3 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s3');
                $s4 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s4');
                $s5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s5');
                $s6 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s6');
                $s7 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s7');
                $surplus = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'surplus');

                $info['seller_pr'] = '用户获利:'.($s0['info']/100).'元,用户上级获利:'.($s1['info']/100).'元,用户上上级获利:'.($s1_5['info']/100).'元,商家获利:'.($s2['info']/100).'元,店小二获利:'.($s3['info']/100).'元,个人创业获利:'.($s4['info']/100).'元,城市服务中心获利:'.($s5['info']/100).'元,省服务中心获利:'.($s6['info']/100).'元,有车有保获利:'.(($s7['info']+$surplus['info'])/100).'元';

                if(!$result['status']){
                    $this->error('获取订单统计数据失败', null, $result['info']);
                }
                $info['seller_name'] = $result['info']['name'];
                $this->success('获取订单统计数据成功', null, $info);
            }else{
                $this->error('获取订单统计数据失败', null, $result['info']);
            }
        }
    }


    public function exportSellerOrderExcel()
    {

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $page = ['curpage' => 1, 'size' => 99999];
            $date = $this->_param('date');
            $dateType = $this->_param('dateType');
            $seller_id = $this->_param('seller_id');
            switch ($dateType) {
                case 'day':
                    $start = strtotime(date('Y-m-d', $date));
                    $end = strtotime('next day', $start);
                    break;
                case 'week':
                    $start = strtotime(date('Y-m-d', $date));
                    $end = strtotime('next week', $start);
                    break;
                case 'month':
                    $start = strtotime(date('Y-m-d', $date));
                    $end = strtotime('next month', $start);
                    break;
                case 'daterange':
                    $date = explode(",", $date);
                    $start = $date[0];
                    $end = $date[1];
                    break;
                default:
                    $this->error('dateType 错误');
                    break;
            }

            $map = [
                'so.seller_id' => ['=', $seller_id],
                'so.create_time' => [['>=', $start], ['<', $end]],
                'so.order_status' => ['=', SellerOrderStatusEmnu::PAY]
            ];
            $order = [
                'so.create_time' => 'desc'
            ];
            $result = (new SellerLogic)->querySellerOrder($map, $page, $order);

            if($result['status']){
                $data = [];

                $head = ['#', ['用户昵称',20], ['用户手机号',25], '订单金额(元)', '实付金额(元)', '不优惠金额(元)', '商家实收(元)', ['创建时间',20], '是否首次消费','支付类型','用户获利','用户上级获利','用户上上级获利','商家获利','店小二获利','个人创业获利','城市服务中心获利','省服务中心获利','有车有保获利'];
                $i = 1;

                //获取商家开户行 银行卡号
                $info = $result['info'];

                foreach ($info['list'] as &$item){
                    $orders[] = $item['id'];

                    $s0 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s0');
                    $s1 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s1');
                    $s1_5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s1_5');
                    $s2 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s2');
                    $s3 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s3');
                    $s4 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s4');
                    $s5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s5');
                    $s6 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s6');
                    $s7 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'s7');
                    $surplus = (new ProfitLogic)->sum(['type'=>2,'order_id'=>$item['id']],'surplus');
                    $item['s0'] = $s0['info'];
                    $item['s1'] = $s1['info'];
                    $item['s1_5'] = $s1_5['info'];
                    $item['s2'] = $s2['info'];
                    $item['s3'] = $s3['info'];
                    $item['s4'] = $s4['info'];
                    $item['s5'] = $s5['info'];
                    $item['s6'] = $s6['info'];
                    $item['s7'] = $s7['info']+$surplus['info'];
                    $item['seller_pr'] = '用户获利:'.($s0['info']/100).'元,用户上级获利:'.($s1['info']/100).'元,用户上上级获利:'.($s1_5['info']/100).'元,商家获利:'.($s2['info']/100).'元,店小二获利:'.($s3['info']/100).'元,个人创业获利:'.($s4['info']/100).'元,城市服务中心获利:'.($s5['info']/100).'元,省服务中心获利:'.($s6['info']/100).'元,有车有保获利:'.(($s7['info']+$surplus['info'])/100).'元';
                }

                $s0 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s0');
                $s1 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s1');
                $s1_5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s1_5');
                $s2 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s2');
                $s3 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s3');
                $s4 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s4');
                $s5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s5');
                $s6 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s6');
                $s7 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s7');
                $surplus = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'surplus');

                $foot = '用户获利:'.($s0['info']/100).'元,用户上级获利:'.($s1['info']/100).'元,用户上上级获利:'.($s1_5['info']/100).'元,商家获利:'.($s2['info']/100).'元,店小二获利:'.($s3['info']/100).'元,个人创业获利:'.($s4['info']/100).'元,城市服务中心获利:'.($s5['info']/100).'元,省服务中心获利:'.($s6['info']/100).'元,有车有保获利:'.(($s7['info']+$surplus['info'])/100).'元';
                foreach ($info['list'] as $val){
                    $val['is_first'] = $val['is_first'] == 1 ? '是' : '否';
                    switch ($val['type']) {
                        case 1:
                            $val['type'] = '支付宝';
                            break;
                        case 2:
                            $val['type'] = '微信公众号';
                            break;
                        case 22:
                            $val['type'] = '微信APP';
                            break;
                        default:
                            $val['type'] = '未知';
                            break;
                    };
                    $data[] = [
                        $i,
                        $val['nickname'],
                        ' '.$val['mobile'],
                        intval($val['amount']) / 100,
                        intval($val['price']) / 100,
                        intval($val['no_discount']) / 100,
                        intval($val['income']) / 100,
                        date('Y-m-d H:i:s', $val['create_time']),
                        $val['is_first'],
                        $val['type'],
                        intval($val['s0']) /100,
                        intval($val['s1']) /100,
                        intval($val['s1_5']) /100,
                        intval($val['s2']) /100,
                        intval($val['s3']) /100,
                        intval($val['s4']) /100,
                        intval($val['s5']) /100,
                        intval($val['s6']) /100,
                        intval($val['s7']) /100
                    ];
                    $i++;
                }

            }else{
                return 'error';
            }


            (new \excel\Excel())->createExcel($head, $data, '商家订单明细'.date('Y-m-d H:i:s'),$foot);

    }

    public function exportSumListExcel()
    {
        if(!$this->isBackAdmin()){
            $this->error('没有权限查看此内容');
        }

        $page = ['curpage' => 1, 'size' => 99999];
        $date = $this->_param('date');
        $dateType = $this->_param('dateType');

        switch ($dateType) {
            case 'day':
                $start = strtotime(date('Y-m-d', $date));
                $end = strtotime('next day', $start);
                break;
            case 'week':
                $start = strtotime(date('Y-m-d', $date));
                $end = strtotime('next week', $start);
                break;
            case 'month':
                $start = strtotime(date('Y-m-d', $date));
                $end = strtotime('next month', $start);
                break;
            case 'daterange':
                $date = explode(",", $date);
                $start = $date[0];
                $end = $date[1];
                break;
            default:
                $this->error('dateType 错误');
                return 'dateType 错误';
        }

        $map = [
            'so.create_time' => [['>=', $start], ['<', $end]],
            'so.order_status' => ['=', SellerOrderStatusEmnu::PAY]
        ];
        $order = [
            'order_count' => 'desc'
        ];
        $result = (new SellerLogic)->querySellerOrderSum($map, $page, $order);
        if($result['status']){
            $data = [];

            $head = ['#', ['商家名称',20], ['联系电话',25], '订单数', '订单总额  ', '实付总额', '实收总额', '首次优惠金额(元)', '首次优惠总额(元)', ['开户银行',20], ['银行卡号',30],'用户获利','用户上级获利','用户上上级获利','商家获利','店小二获利','个人创业获利','城市服务中心获利','省服务中心获利','有车有保获利'];
            $i = 1;

            //获取商家开户行 银行卡号
            $info = $result['info'];
            $SellerLogic = new SellerLogic;
            foreach ($info['list'] as $val){
                $result = $SellerLogic->getSellerInfo($val['seller_id']);
                if($result['status']){
                    $val['bank_card_name'] = $result['info']['bank_card_name'];
                    $val['bank_card_no'] = $result['info']['bank_card_no'];
                }

                $map_order = [
                    'so.seller_id' => ['=', $val['seller_id']],
                    'so.create_time' => [['>=', $start], ['<', $end]],
                    'so.order_status' => ['=', SellerOrderStatusEmnu::PAY]
                ];
                $result2 = $SellerLogic->querySellerOrder($map_order, ['curpage' => 1, 'size' => 999999]);

                $orders = [];
                foreach ($result2['info']['list'] as $item){
                    $orders[] = $item['id'];
                }
                $s0 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s0');
                $s1 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s1');
                $s1_5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s1_5');
                $s2 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s2');
                $s3 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s3');
                $s4 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s4');
                $s5 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s5');
                $s6 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s6');
                $s7 = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'s7');
                $surplus = (new ProfitLogic)->sum(['type'=>2,'order_id'=>['in', join(',', $orders)]],'surplus');

                $val['s0'] = $s0['info'];
                $val['s1'] = $s1['info'];
                $val['s1_5'] = $s1_5['info'];
                $val['s2'] = $s2['info'];
                $val['s3'] = $s3['info'];
                $val['s4'] = $s4['info'];
                $val['s5'] = $s5['info'];
                $val['s6'] = $s6['info'];
                $val['s7'] = $s7['info']+$surplus['info'];

                $data[] = [
                    $i,
                    $val['name'],
                    ' '.$val['contact_number'],
                    $val['order_count'],
                    intval($val['sum_amount']) / 100,
                    intval($val['sum_price']) / 100,
                    intval($val['sum_income']) / 100,
                    intval($val['first_offer']) / 100,
                    intval($val['sum_amount'] - $val['sum_price']) / 100,
                    $val['bank_card_name'],
                    ' '.$val['bank_card_no'],
                    intval($val['s0']) /100,
                    intval($val['s1']) /100,
                    intval($val['s1_5']) /100,
                    intval($val['s2']) /100,
                    intval($val['s3']) /100,
                    intval($val['s4']) /100,
                    intval($val['s5']) /100,
                    intval($val['s6']) /100,
                    intval($val['s7']) /100
                ];
                $i++;
            }

        }else{
            return 'error';
        }


        (new \excel\Excel())->createExcel($head, $data, '商家订单统计'.date('Y-m-d H:i:s'));
    }

    /**
     * 获取所有商家类目
     */
    public function getAllCategory()
    {
        if(!$this->isBackAdmin()){
            $this->error('没有权限查看此内容');
        }
        $result = (new SellerCategoryLogic)->queryAllCategory();
        if($result['status']){
            $this->success('获取商家类目成功', null, $result['info']);
        }else{
            $this->error('获取商家类目失败,', null, $result['info']);
        }
    }

    /**
     * 添加商家类目
     */
    public function addCategory()
    {
        if(IS_AJAX) {
            if (!$this->isBackAdmin()) {
                $this->error('没有权限查看此内容');
            }
            $name          = $this->_param('name');
            $parent_id     = $this->_param('parent_id');
            $note          = $this->_param('note');
            $display_order = $this->_param('display_order');
            $icon          = $this->_param('icon');
            if (empty($parent_id)) $this->error('parent_id 缺失');
            if (empty($name)) $this->error('name 缺失');
            if (empty($display_order)) $display_order = 0;
            if (empty($icon)) $icon = 0;

            $result = (new SellerCategoryLogic)->addCategory($name, $parent_id, $note, $display_order, $icon);
            if($result['status']){
                $this->success('添加商家类目成功', null);
            }else{
                $this->error('添加商家类目失败', null, $result['info']);
            }
        }
    }

    /**
     * 编辑商家类目
     */
    public function editCategory()
    {
        if(IS_AJAX) {
            if (!$this->isBackAdmin()) {
                $this->error('没有权限查看此内容');
            }
            $cate_id       = $this->_param('cate_id');
            $name          = $this->_param('name');
            $parent_id     = $this->_param('parent_id');
            $note          = $this->_param('note');
            $display_order = $this->_param('display_order');
            $icon          = $this->_param('icon');
            if (empty($cate_id)) $this->error('cate_id 缺失');
            if (empty($name)) $name = false;
            if (empty($parent_id)) $parent_id = false;
            if (empty($display_order)) $display_order = false;
            if (empty($icon)) $icon = 0;

            $result = (new SellerCategoryLogic)->updateCategory($cate_id, $name, $parent_id, $note, $display_order, $icon);
            if($result['status']){
                $this->success('编辑商家类目成功', null);
            }else{
                $this->error('编辑商家类目失败', null, $result['info']);
            }
        }
    }

    /**
     * 删除分类
     */
    public function removeCategory()
    {
        if(IS_AJAX) {
            if (!$this->isBackAdmin()) {
                $this->error('没有权限查看此内容');
            }
            $cate_id       = $this->_param('cate_id');
            if (empty($cate_id)) $this->error('cate_id 缺失');

            $result = (new SellerCategoryLogic)->pretendDelete(['id' => $cate_id]);
            if($result['status']){
                $this->success('删除商家类目成功', null);
            }else{
                $this->error('删除商家类目失败', null, $result['info']);
            }
        }
    }

    /**
     * ajax - seller-select
     */
    public function select(){

        $map['name'] = array('like', "%" . $this->_param('q', '', 'trim') . "%");
        //$map['nickname'] = array('like', "%" . $q = $this->_param('q',-1)  . "%");

        $q = $this->_param('q',-1);
        if(!empty($q) && $q != -1&&is_numeric($q)){
            $map = [];
            $map['id'] = $q;
        }

        $page = array('curpage'=>0,'size'=>20);
        $order = "id desc";
        $result = (new SellerLogic())->query($map,$page, $order,false,'id,name,main_img_small');

        if($result['status']){
            $list = $result['info']['list'];
            foreach($list as $key=>$g){
                $list[$key]['head'] = getImgUrl($list[$key]['main_img_small'],60);
            }

            $this->success("读取成功","",$list);
        }

    }
    /**
     * 编辑首次优惠次数
     */
    public function editNum()
    {
        if(IS_AJAX){
            $seller_id = $this->_param('seller_id');
            $num = $this->_param('num');
            if(empty($seller_id)) $this->error('商家id缺失');


            $result = (new SellerLogic)->saveByID($seller_id, ['first_offer_num' => $num]);
            if($result['status']){
                $this->success('修改成功', null, $result['info']);
            }else{
                $this->error('修改失败', null, $result['info']);
            }
        }
    }


}